// pages/bilibili/bilibili.js
Page({

  /**
   * Page initial data
   */
  data: {
    videoList: [],
    currentPage: 1,
    totalPage: 0
  },
  onReachBottom: function(e) {
    let _this = this;
    let currentPage = _this.data.currentPage;
    let totalPage = _this.data.totalPage;
    console.log(currentPage, totalPage)
    //判断当前页数和总页数是否相等，如果相等则无需查询
    if (currentPage === totalPage) {
      wx.showToast({
        title: '已经是全部啦~~~',
        icon: 'none'
      })
      return;
    }

    let page = currentPage + 1;
    _this.setData({
      currentPage: page
    })
    this.getList(page);
  },
  onPullDownRefresh: function() {
    let _this = this
    wx.showNavigationBarLoading();
    this.setData({
      videoList: [],
      currentPage: 1,
    })
    this.getList(1);

  },
  onLoad: function (options) {
    this.getList(1)
  },
  /**
   * Lifecycle function--Called when page load
   */
  getList(page) {
    wx.showLoading({
      title: '努力加载中...',
    })
    console.log('page,', page)
    let _this = this
    wx.request({
header: app.globalData.header,
      url: `https://space.bilibili.com/ajax/member/getSubmitVideos?mid=32820037&pagesize=30&tid=0&page=${page}&keyword=&order=pubdate`,
      method: 'get',
      success(res) {
        wx.hideLoading()
        wx.hideNavigationBarLoading()
        wx.stopPullDownRefresh()
        console.log('res', res)
        let data = res.data.data.vlist
        let videoList = data;
        let newList = _this.data.videoList;
        _this.setData({
          videoList: newList.concat(videoList),
          totalPage: res.data.data.pages,
        })
        console.log('videoList', res.data.data.vlist)
      },
    })
  },
  
  moveToLocation() {
    wx.navigateTo({
      url: '/pages/index/index',
    })
  },
  toVideo(e) {
    let _this = this
    let entity = _this.data.videoList[e.target.dataset.arrindex]
    let av = entity.aid
    let desc = encodeURIComponent(entity.description)
    let title = encodeURIComponent(entity.title)
    wx.request({
header: app.globalData.header,
      url: `https://api.bilibili.com/playurl?callback=callbackfunction&aid=${av}&page=1&platform=html5&quality=3&vtype=mp4&type=jsonp`,
      header: {
        Cookie: `CURRENT_FNVAL=16; buvid3=A5866C41-3EB6-431C-92A7-BDED443B21CA110263infoc; sid=adalxz04; stardustvideo=1; LIVE_BUVID=AUTO7615553074499458; fts=1555474025; UM_distinctid=16a43719d16560-0c6616bd7adf7e-36697e04-384000-16a43719d178cf; rpdid=|(JY)RJ))|~m0J'ullY|ulRlm; CURRENT_QUALITY=80; im_notify_type_267859889=0; DedeUserID=267859889; DedeUserID__ckMd5=06773895ee640544; SESSDATA=9f37594d%2C1560787192%2C19e7cb51; bili_jct=362f53407c37211fa1e8618736c72284; finger=1e7fd4f8; _dfcaptcha=5de5d45155081dcdbc0a6369295d5d61; bp_t_offset_267859889=261534946796702184`
      },
      success(res) {
        let entity = JSON.parse(res.data.replace('callbackfunction(', '').replace(');', ''))
        let videoSrc = encodeURIComponent(entity.durl[0].url.replace("http", "https"))
        wx.navigateTo({
          url: `/pages/webview/webview?url=${videoSrc}&desc=${desc}&title=${title}`,
        })
      }
    })

  },
  /**
   * Lifecycle function--Called when page is initially rendered
   */
  onReady: function() {

  },

  /**
   * Lifecycle function--Called when page show
   */
  onShow: function() {

  },

  /**
   * Lifecycle function--Called when page hide
   */
  onHide: function() {

  },

  /**
   * Lifecycle function--Called when page unload
   */
  onUnload: function() {

  },


  /**
   * Called when user click on the top right corner to share
   */
  onShareAppMessage: function() {

  }
})